#include <stdio.h>
#include "air105.h"
#include "sysc.h"
#include "delay.h"
#include "usart.h"
#include "rtthread.h"
void IO_Init(void);
void thread1_entry(void* parameter);
void thread2_entry(void* parameter);
int main(void)
{
    IO_Init();
    rt_thread_t thread1_ptr,thread2_ptr;
    thread1_ptr = rt_thread_create("thread2",thread1_entry, RT_NULL,512, 30, 25);
    if (thread1_ptr != RT_NULL) {
        rt_thread_startup(thread1_ptr);
    } else {
        rt_kprintf("error");
    }

    thread2_ptr = rt_thread_create("thread2",thread2_entry, RT_NULL,512, 1, 25);
    if (thread2_ptr != RT_NULL) {
        rt_thread_startup(thread2_ptr);
    } else {
        rt_kprintf("error");
    }
}


void thread1_entry(void* parameter) {
    int MAX = 20;
    while(1)
    {
        for(int i = 0; i<MAX; i++) {
            GPIO_SetBits(GPIOD, GPIO_Pin_15);
            rt_thread_mdelay(i);
            GPIO_ResetBits(GPIOD, GPIO_Pin_15);
            rt_thread_mdelay(MAX-i);
        }

        for(int i = 0; i<MAX; i++) {
            GPIO_ResetBits(GPIOD, GPIO_Pin_15);
            rt_thread_mdelay(i);
            GPIO_SetBits(GPIOD, GPIO_Pin_15);
            rt_thread_mdelay(MAX-i);
        }
    }

}

void thread2_entry(void* parameter) {

    while(1)
    {
        GPIO_ResetBits(GPIOD, GPIO_Pin_14);
        rt_thread_mdelay(1000);
        GPIO_SetBits(GPIOD, GPIO_Pin_14);
        rt_thread_mdelay(1000);
    }

}


void IO_Init(void) {
    GPIO_InitTypeDef gpio;
    gpio.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
    gpio.GPIO_Mode = GPIO_Mode_Out_PP;
    gpio.GPIO_Remap = GPIO_Remap_1;
    GPIO_Init(GPIOD, &gpio);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif
